package com.qfedu.yanglao20admin.article.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.yanglao20.common.CommonResult;
import com.qfedu.yanglao20.common.annotation.LogInfo;
import com.qfedu.yanglao20.common.param.PageParam;
import com.qfedu.yanglao20.common.utils.OssUtils;
import com.qfedu.yanglao20.entity.article.Article;
import com.qfedu.yanglao20admin.article.param.ArticleParam;
import com.qfedu.yanglao20admin.article.service.ArticleService;
import com.qfedu.yanglao20admin.article.vo.ArticleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@Api(tags = "文章内容管理")
@RestController
@RequestMapping("/article")
public class ArticleController {


    @Resource
    private ArticleService articleService;

    @LogInfo
    @ApiOperation(value = "分页展示文章内容", notes = "分页展示文章内容")
    @GetMapping("/list")
    public CommonResult<PageParam> page(ArticleParam param) {
        Page<ArticleVO> articles = articleService.pageArticle(param);
        return CommonResult.success(articles);
    }

    @LogInfo
    @ApiOperation(value = "根据id删除文章内容", notes = "根据id删除文章内容")
    @GetMapping("/delete")
    public CommonResult deleteArticle(@RequestParam @ApiParam("文章内容id") Integer id) {
        articleService.deleteById(id);
        return CommonResult.success();
    }

    @LogInfo
    @ApiOperation(value = "添加文章内容", notes = "添加文章内容")
    @PostMapping("/add")
    public CommonResult<Article> addArticle(Article article) {
        articleService.addArticle(article);
        return CommonResult.success();
    }

    @LogInfo
    @ApiOperation(value = "根据id查询文章内容", notes = "根据id查询文章内容")
    @GetMapping("/query")
    public CommonResult queryArticle(@RequestParam @ApiParam("文章内容id") Integer id) {
        Article article = articleService.queryArticle(id);
        return CommonResult.success(article);
    }

    @LogInfo
    @ApiOperation(value = "修改文章内容", notes = "修改文章内容")
    @PostMapping("/update")
    public CommonResult<Article> updateArticle(Article article) {
        articleService.updateArticle(article);
        return CommonResult.success();
    }

    @LogInfo
    @ApiOperation(value = "根据id查询指定文章详细内容", notes = "根据id查询文章详细内容")
    @GetMapping("/select")
    public CommonResult selectArticle(@RequestParam @ApiParam("文章内容id") Integer id) {
        Article article = articleService.selectByArticleId(id);
        return CommonResult.success(article);
    }

    @LogInfo
    @ApiOperation(value = "上传文章封面", notes = "上传文章封面")
    @PostMapping("/upload")
    public String upload(MultipartFile file) {

        String path = null;
        try {
            path = OssUtils.uploadFile2OSS(file.getInputStream(), file.getOriginalFilename());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return path;
    }
}

